<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>公司管理</el-breadcrumb-item>
      <el-breadcrumb-item>岗位列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="3">
          <el-select
            v-model="companyId"
            placeholder="请选择"
            @change="getPosition"
          >
            <el-option
              v-for="item in companyList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddPage">添加岗位</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table @expand-change="expandSelect" ref="refTable" :data="positionList" border stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="联系电话：">
                <span>{{ scope.row.phone }}</span>
              </el-form-item>
              <el-form-item label="创建时间：">
                <span>{{ scope.row.time }}</span>
              </el-form-item>
              <el-form-item label="岗位名称：">
                <span>{{ scope.row.name }}</span>
              </el-form-item>
              <el-form-item label="岗位 ID：">
                <span>{{ scope.row.id }}</span>
              </el-form-item>
              <el-form-item label="公司名字：">
                <span>{{ scope.row.companyName }}</span>
              </el-form-item>
              <el-form-item label="公司 ID：">
                <span>{{ scope.row.positionCompanyId }}</span>
              </el-form-item>
            </el-form>
            <el-row>
              <el-col v-for="(item,i) in scope.row.welfareList" :key="item" :span="24">
                <el-tag closable @close="handleClose(scope.row,i)" type="primary">{{item}}</el-tag>
              </el-col>
              <el-col :span="12">
                <el-input
                  v-if="inputVisible"
                  v-model="inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row.id,scope.row.welfareList)"
                  @blur="handleInputConfirm(scope.row.id,scope.row.welfareList)"
                ></el-input>
                <el-button v-else size="small" @click="showInput" type="primary" plain>+ 增加描述</el-button>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="岗位名称" prop="name"></el-table-column>
        <el-table-column label="联系邮箱" prop="email"></el-table-column>
        <el-table-column label="联系电话" prop="phone"></el-table-column>
        <el-table-column label="热度" width="155px">
          <template slot-scope="scope">
            <el-input-number
              v-model="scope.row.heat"
              @change="changeHeat(scope.row.id,scope.row.heat)"
              :min="0"
              :max="100"
              size="small"
              :disabled="$store.getters.iftype3"
              :controls="false"
            ></el-input-number>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="220px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="showBox(scope.row)">修改</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delCompanyById(scope.row.id)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 修改岗位信息 -->
    <el-dialog title="修改公司信息" :visible.sync="showPutBox" width="30%" @close="putClose">
      <el-form :model="putFrom" :rules="putRules" ref="putRef" label-width="100px">
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="putFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="联系邮箱" prop="email">
          <el-input v-model="putFrom.email"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="putFrom.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showPutBox = false">取 消</el-button>
        <el-button type="primary" @click="putData">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      companyList: [],
      companyId: '',
      positionList: [],
      showPutBox: false,
      putFrom: {},
      putRules: {
        name: [{ required: true, message: '请输入岗位名称', trigger: 'blur' }],
        email: [{ required: true, message: '请输入联系邮箱', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
      },
      inputVisible: false,
      inputValue: ''
    }
  },
  created() {
    this.getCompany()
  },
  methods: {
    async getCompany() {
      const { data: res } = await this.$http.get('/company', {
        params: {
          pageSize: -1,
          pageNum: -1
        }
      })
      if (res.meta.status != 200) {
        return this.$message.error('获取公司列表失败！')
      }
      this.companyList = res.data.list
      this.companyId = this.companyList[0].id
      this.getPosition()
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage
      this.getUserList()
    },
    async getPosition() {
      const { data: res } = await this.$http.get(`/company/${this.companyId}`)
      if (res.meta.status != 200) {
        return this.$message.error('获取岗位列表失败！')
      }
      this.positionList = res.data.positionList
    },
    goAddPage() {
      this.$router.push('/position/add')
    },
    async delCompanyById(id) {
      this.$confirm('此操作将永久删除该岗位, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete('/position', { data: { id: id } })
          if (res.meta.status != 200) {
            return this.$message.error('删除岗位失败！')
          }
          this.$message.success('删除岗位成功！')
          this.getPosition()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 修改热度
    async changeHeat(id, heat) {
      const { data: res } = await this.$http.put('position', { id: id, heat: heat })
      if (res.meta.status != 200) {
        return this.$message.error('修改热度失败！')
      }
      this.$message.success('修改热度成功！')
      this.getCompany()
    },
    // 打开修改岗位 box
    showBox(data) {
      this.showPutBox = true
      this.putFrom = data
    },
    // 关闭修改 Box
    putClose() {
      this.showPutBox = false
      this.$refs.putRef.resetFields()
    },
    // 提交修改岗位
    async putData() {
      const { data: res } = await this.$http.put('position', this.putFrom)
      if (res.meta.status != 200) {
        return this.$message.error('修改岗位失败！')
      }
      this.$message.success('修改岗位成功！')
      this.getPosition()
      this.showPutBox = false
    },
    // 修改描述
    async handleClose(data, i) {
      data.welfareList.splice(i, 1)
      const { data: res } = await this.$http.put('position', data)
      if (res.meta.status != 200) {
        return this.$message.error('修改描述失败！')
      }
      this.$message.success('修改描述成功！')
    },
    // 控制手风琴效果
    expandSelect(row, expandedRows) {
      if (expandedRows.length > 1) {
        this.expands = []
        if (row) {
          this.expands.push(row)
        }
        this.$refs.refTable.toggleRowExpansion(expandedRows[0])
      } else {
        this.expands = []
      }
    },
    // 新增描述
    async handleInputConfirm(id, list) {
      list.push(this.inputValue)
      if (this.inputValue) {
        const { data: res } = await this.$http.put('position', { id: id, welfareList: list })
        if (res.meta.status != 200) {
          return this.$message.error('新增描述失败！')
        }
        this.$message.success('新增描述成功！')
      }
      this.inputVisible = false
      this.inputValue = ''
    },
    // 展示添加描述input
    showInput() {
      this.inputVisible = true
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus()
      })
    }
  }
}
</script>

<style lang="less" scoped>
.el-col {
  margin-top: 5px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>